package com.achievement.service.impl.authorImpl;


import com.achievement.domain.bo.author.CopyRightAuthorBO;
import com.achievement.domain.dto.author.CopyRightAuthorDTO;
import com.achievement.domain.entity.author.CopyRightAuthorEntity;
import com.achievement.domain.vo.author.CopyRightAuthorVO;
import com.achievement.mapper.author.CopyRightAuthorMapper;
import com.achievement.mappings.author.CopyRightAuthorMapping;
import com.achievement.service.author.ICopyRightAuthorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class CopyRightAuthorServiceImpl extends ServiceImpl<CopyRightAuthorMapper, CopyRightAuthorEntity> implements ICopyRightAuthorService {

    @Autowired
    private CopyRightAuthorMapping mapping;


    @Override
    public List<CopyRightAuthorEntity> getListByConditions(CopyRightAuthorDTO authorDTO) {
        QueryWrapper<CopyRightAuthorEntity> queryWrapper = new QueryWrapper();
        List<CopyRightAuthorEntity> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public CopyRightAuthorVO getInfoById(String id) {
        CopyRightAuthorEntity entity = this.getById(id);
        return mapping.entity2Vo(entity);
    }

    @Override
    public boolean addOrUpdate(List<CopyRightAuthorDTO> list,String copyrightId) {
        //获取该专利下的现有所有发明人
        CopyRightAuthorDTO dto = new CopyRightAuthorDTO();
        dto.setCopyrightId(copyrightId);
        List<CopyRightAuthorEntity> copyrightAuthorEntityList = getListByConditions(dto);
        Set<String> existAuthorId = copyrightAuthorEntityList.stream().map(a -> a.getPersonId()).collect(Collectors.toSet());
        Set<String> authorId = list.stream().map(a -> a.getPersonId()).collect(Collectors.toSet());
        //取交集获取需要更新的数据。
        List<CopyRightAuthorDTO> updateList = list.stream()
                .filter(a -> existAuthorId.contains(a.getPersonId()))
                .collect(Collectors.toList());
        //取list中personId不存在于 existAuthorId 的差集获取需要添加的数据。
        List<CopyRightAuthorDTO> addList = list.stream()
                .filter(a -> !existAuthorId.contains(a.getPersonId()))
                .collect(Collectors.toList());
        //取 copyrightAuthorEntityList 中personId不存在于list 的差集获取需要删除的数据。
        List<CopyRightAuthorEntity> delList = copyrightAuthorEntityList.stream()
                .filter(a -> !authorId.contains(a.getPersonId()))
                .collect(Collectors.toList());
        //dto->bo
        List<CopyRightAuthorBO> updateBoList = updateList.stream().map(mapping::dto2Bo).collect(Collectors.toList());
        List<CopyRightAuthorBO> addBoList = addList.stream().map(mapping::dto2Bo).collect(Collectors.toList());
        //bo->entity
        List<CopyRightAuthorEntity> updateAuthorList = updateBoList.stream().map(mapping::bo2Entity).collect(Collectors.toList());
        List<CopyRightAuthorEntity> addAuthorList = addBoList.stream().map(mapping::bo2Entity).collect(Collectors.toList());
        List<CopyRightAuthorEntity> saveOrUpdateList = new ArrayList<>();
        saveOrUpdateList.addAll(updateAuthorList);
        saveOrUpdateList.addAll(addAuthorList);

        return this.saveOrUpdateBatch(saveOrUpdateList) && this.removeByIds(delList);
    }

    @Override
    public boolean add(CopyRightAuthorDTO dto) {
        CopyRightAuthorEntity entity = mapping.dto2Entity(dto);
        return this.updateById(entity);
    }

    @Override
    public boolean addBatch(List<CopyRightAuthorDTO> list,String copyrightId) {
        this.batchRemoveByCopyRightIds(Arrays.asList(copyrightId));
        list.forEach(a->{
            a.setCopyrightId(copyrightId);
        });
        List<CopyRightAuthorEntity> addList = list.stream().map(mapping::dto2Entity)
                .collect(Collectors.toList());
        return this.saveBatch(addList, addList.size());
    }

    @Override
    public boolean update(CopyRightAuthorDTO dto) {
        CopyRightAuthorEntity entity = mapping.dto2Entity(dto);
        return this.updateById(entity);
    }

    @Override
    public boolean batchRemove(List<String> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public List<CopyRightAuthorVO> getListByCopyRightId(String copyrightId) {
        LambdaQueryWrapper<CopyRightAuthorEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(CopyRightAuthorEntity::getCopyrightId, copyrightId);
        queryWrapper.orderByAsc(CopyRightAuthorEntity::getOrderId);

        List<CopyRightAuthorEntity> list= this.list(queryWrapper);
        return list.stream().map(mapping::entity2Vo).collect(Collectors.toList());

    }

    @Override
    public boolean batchRemoveByCopyRightIds(List<String> copyrightIds) {
        LambdaQueryWrapper<CopyRightAuthorEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(CopyRightAuthorEntity::getCopyrightId, copyrightIds);
        return this.remove(queryWrapper);
    }
}
